package univ.bme.accounting.beans;

import javax.ejb.EJB;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.bean.ManagedBean;

import univ.bme.accounting.dao.CategoryDAO;
import univ.bme.accounting.domain.Category;

@ManagedBean(name = "categoryConverterBean") 
@FacesConverter(value = "categoryConverter")
public class CategoryConverter implements Converter {

	@EJB
	private CategoryDAO categorydao;

    @Override
    public Object getAsObject(FacesContext ctx, UIComponent component, String value) {
      return categorydao.find(new Integer(value)); 
    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object o) {
        //This will return view-friendly output for the dropdown menu
    	Category cat = (Category) o;
    	return String.valueOf(cat.getId());
    }
}